﻿# coding=utf-8
'''
Created on 2012-6-20

@author: wb_suoyao.shen
'''
import hashlib
from django.views.decorators.csrf import csrf_exempt
from django.shortcuts import render_to_response
from ssyblog.admin.models import AboutMe

def index(request):
    try:
        user = request.session['user']
    except KeyError:
        return render_to_response('cms/login.html')
    if user:
        return render_to_response('cms/index.html',locals())
    else:
        return render_to_response('cms/login.html')

def login(request):
    return render_to_response('cms/login.html')

@csrf_exempt
def signup(request):
    admin = request.POST['uname']
    password = request.POST['upwd']
    if admin and password:
        userSet = AboutMe.objects.filter(name = admin)
        if not userSet:
            tips = ('user ' + admin + ' is not exists')
            return render_to_response('cms/login.html',locals())
        user = userSet[0]
        newpwd = hashlib.new('md5', password).hexdigest()
        if newpwd != user.password:
            tips = ('the password is error')
            return render_to_response('cms/login.html',locals())
        request.session['user'] = admin
        return render_to_response('cms/index.html', locals())
    else:
        tips = ('user or password is empty')
        return render_to_response('cms/login.html', locals())
    
def signout(request):
    try:
        del request.session['user']
        request.session.set_expiry(0)
    except KeyError:
        pass
    return render_to_response('cms/login.html')
